package com.example.photograph.service.impl;

import com.example.photograph.domain.SysOrderItem;
import com.example.photograph.domain.VoPerInfo;
import com.example.photograph.mapper.SysOrderItemMapper;
import com.example.photograph.service.ISysOrderItemService;
import com.example.photograph.utils.Convert;
import com.example.photograph.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单详情Service业务层处理
 * 
 * @author wuyj
 * @date 2021-03-06
 */
@Service
public class SysOrderItemServiceImpl implements ISysOrderItemService 
{
    @Autowired
    private SysOrderItemMapper sysOrderItemMapper;

    /**
     * 查询订单详情
     * 
     * @param orderItemId 订单详情ID
     * @return 订单详情
     */
    @Override
    public SysOrderItem selectSysOrderItemById(Long orderItemId)
    {
        return sysOrderItemMapper.selectSysOrderItemById(orderItemId);
    }

    /**
     * 查询订单详情列表
     * 
     * @param sysOrderItem 订单详情
     * @return 订单详情
     */
    @Override
    public List<SysOrderItem> selectSysOrderItemList(SysOrderItem sysOrderItem)
    {
        return sysOrderItemMapper.selectSysOrderItemList(sysOrderItem);
    }

    /**
     * 新增订单详情
     * 
     * @param sysOrderItem 订单详情
     * @return 结果
     */
    @Override
    public int insertSysOrderItem(SysOrderItem sysOrderItem)
    {
        sysOrderItem.setCreateTime(DateUtils.getNowDate());
        return sysOrderItemMapper.insertSysOrderItem(sysOrderItem);
    }

    /**
     * 修改订单详情
     * 
     * @param sysOrderItem 订单详情
     * @return 结果
     */
    @Override
    public int updateSysOrderItem(SysOrderItem sysOrderItem)
    {
        sysOrderItem.setUpdateTime(DateUtils.getNowDate());
        return sysOrderItemMapper.updateSysOrderItem(sysOrderItem);
    }

    /**
     * 删除订单详情对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysOrderItemByIds(String ids)
    {
        return sysOrderItemMapper.deleteSysOrderItemByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除订单详情信息
     * 
     * @param orderItemId 订单详情ID
     * @return 结果
     */
    @Override
    public int deleteSysOrderItemById(Long orderItemId)
    {
        return sysOrderItemMapper.deleteSysOrderItemById(orderItemId);
    }

    /**
     * 批量新增订单详情信息
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int updateOrderItem(Long orderId, Long[] userIds)
    {
        sysOrderItemMapper.deleteSysOrderItemByOrderId(orderId);
        return insertOrderItem(orderId,userIds);
    }

    /**
     * 查询员工工作量列表
     *
     * @return 员工工作量集合
     */
    @Override
    public List<SysOrderItem> selectWorkloadList(SysOrderItem sysOrderItem)
    {
        return sysOrderItemMapper.selectWorkloadList(sysOrderItem);
    }

    /**
     * 个人综合业绩详情
     *
     * @return 个人综合业绩详情集合
     */
    @Override
    public List<VoPerInfo> selectPerInfoByUserId(Long userId)
    {
        return sysOrderItemMapper.selectPerInfoByUserId(userId);
    }

    /**
     * 新增订单详情信息
     *
     */
    private int insertOrderItem(Long orderId, Long[] userIds)
    {
        int rows = 1;
        List<SysOrderItem> list = new ArrayList<SysOrderItem>();
        for (Long userId : userIds)
        {
            if (userId>1000){
                continue;
            }
            SysOrderItem orderItem = new SysOrderItem();
            orderItem.setOrderId(orderId);
            orderItem.setUserId(userId);
            list.add(orderItem);
        }
        if (list.size() > 0)
        {
            rows = sysOrderItemMapper.batchOrderItem(list);
        }
        return rows;
    }
}
